<?php
/**
 * @title 789. 逃脱阻碍者 - 模拟
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param Integer[][] $ghosts
     * @param Integer[] $target
     * @return Boolean
     */
    function escapeGhosts($ghosts, $target) {
        /**
         * @since 2021-08-22 原点
         */
        list($targetX, $targetY) = $target;
        if($targetX == 0 AND $targetY == 0){
            return true;
        } else {}

        /**
         * @since 2021-08-22 阻碍者
         * @since 2021-08-22 去重, 一个位置可能有多少阻碍者
         */
        $ghostArray = [];
        $tmpArray = [];
        foreach ($ghosts as list($ghostX, $ghostY)){
            if(!isset($ghostArray[$ghostX][$ghostY])){
                $ghostArray[$ghostX][$ghostY] = 1;
                $tmpArray[] = [$ghostX, $ghostY];
            } else {}
        }
        $ghosts = $tmpArray;

        /**
         * @since 2021-08-22 移动轨迹
         */
        $moveArray = [[0,0]];

        /**
         * @since 2021-08-22 模拟移动
         */
        while(true){
            /**
             * @since 2021-08-22 阻碍点先移动
             */
            $ghostNextArray = [];
            foreach ($ghosts as list($ghostX, $ghostY)){
                /**
                 * @since 2021-08-22 x+1
                 */
                if(!isset($ghostArray[$ghostX+1][$ghostY])){
                    $ghostArray[$ghostX+1][$ghostY] = 1;
                    $ghostNextArray[] = [$ghostX+1, $ghostY];
                } else {}

                /**
                 * @since 2021-08-22 x-1
                 */
                if(!isset($ghostArray[$ghostX-1][$ghostY])){
                    $ghostArray[$ghostX-1][$ghostY] = 1;
                    $ghostNextArray[] = [$ghostX-1, $ghostY];
                } else {}

                /**
                 * @since 2021-08-22 y+1
                 */
                if(!isset($ghostArray[$ghostX][$ghostY+1])){
                    $ghostArray[$ghostX][$ghostY+1] = 1;
                    $ghostNextArray[] = [$ghostX, $ghostY+1];
                } else {}

                /**
                 * @since 2021-08-22 y-1
                 */
                if(!isset($ghostArray[$ghostX][$ghostY-1])){
                    $ghostArray[$ghostX][$ghostY-1] = 1;
                    $ghostNextArray[] = [$ghostX, $ghostY-1];
                } else {}
            }

            /**
             * @since 2021-08-22 下次
             */
            $ghosts = $ghostNextArray;

            /**
             * @since 2021-08-22 向目的移动
             */
            $moveNextArray = [];
            foreach ($moveArray as list($moveX, $moveY)){
                if($moveX > $targetX){
                    if(!isset($ghostArray[$moveX-1][$moveY])){
                        if($moveX-1 == $targetX AND $moveY == $targetY){
                            return true;
                            break;
                        } else {}

                        $moveNextArray[] = [$moveX-1, $moveY];
                    } else {}
                } elseif($moveX < $targetX) {
                    if(!isset($ghostArray[$moveX+1][$moveY])){
                        if($moveX+1 == $targetX AND $moveY == $targetY){
                            return true;
                            break;
                        } else {}

                        $moveNextArray[] = [$moveX+1, $moveY];
                    } else {}
                }

                if($moveY > $targetY){
                    if(!isset($ghostArray[$moveX][$moveY-1])){
                        if($moveX == $targetX AND $moveY-1 == $targetY){
                            return true;
                            break;
                        } else {}

                        $moveNextArray[] = [$moveX, $moveY-1];
                    } else {}
                } elseif($moveY < $targetY) {
                    if(!isset($ghostArray[$moveX][$moveY+1])){
                        if($moveX == $targetX AND $moveY+1 == $targetY){
                            return true;
                            break;
                        } else {}

                        $moveNextArray[] = [$moveX, $moveY+1];
                    } else {}
                }
            }

            /**
             * @since 2021-08-22 无法继续移动
             */
            if(empty($moveNextArray)){
                return false;
                break;
            } else {
                $moveArray = $moveNextArray;
            }
        }
    }
}

/**
 * @url http://127.0.0.1/leetcode/202108/2021.08.22.php
 */
$datas = [
    [[[1,0],[0,3]], [0,1]], // true
    [[[1,1],[0,3]], [0,1]], // false
    // [[[1,0],[0,3]], [0,0]], // true
    // [[[1,0]], [2,0]], // false
    // [[[2,0]], [1,0]], // false
    // [[[5,0],[-10,-2],[0,-5],[-2,-2],[-7,1]], [7,7]], // false
    // [[[-1,0],[0,1],[-1,0],[0,1],[-1,0]], [0,0]], // true
    // [[[14,5],[35,-16],[31,-12],[14,5],[21,-2],[6,7],[37,-18],[6,7],[34,-15],[-18,-17]], [9,-19]], //
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    $result = $obj->escapeGhosts(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
